package com.app.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.app.model.FwArea;
import com.app.model.FwStreet;
import com.app.service.IFwStreetService;
import com.app.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 房屋街道 前端控制器
 * </p>
 *
 * @author yoominic
 * @since 2025-02-27
 */
@Api(tags = "房屋街道管理")
@RestController
@RequestMapping("/fwStreet")
public class FwStreetController {

    @Autowired
    private IFwStreetService fwStreetService;

    @ApiOperation(value = "获取房屋街道列表", notes = "获取房屋街道列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "keyword", value = "搜索关键字", required = false, paramType = "query", dataType = "string")
    })
    @GetMapping("/getStreetList")
    public Result getStreetList(Integer page, Integer limit, String keyword) {
        Page<FwStreet> pageList = new Page<>(page, limit);
        QueryWrapper<FwStreet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        if(keyword!= null && !keyword.equals("")){
            queryWrapper.like("area_name", keyword);
        }
        queryWrapper.orderByAsc("create_time");

        Page<FwStreet> pageInfo = fwStreetService.page(pageList, queryWrapper);
        JSONObject result = new JSONObject();
        result.put("list", pageInfo.getRecords());
        result.put("total", pageInfo.getTotal());
        return Result.success(result);
    }

    @ApiOperation(value = "保存房屋街道信息", notes = "保存房屋街道信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fwStreet", value = "房屋片区对象", required = true, paramType = "body", dataType = "FwStreet")
    })
    @PostMapping("/saveStreet")
    public Result saveStreet(@RequestBody FwStreet fwStreet) {
        Integer loginUserId = StpUtil.getLoginIdAsInt();
        if (fwStreet.getId() == null){
            fwStreet.setStatus(1);
            fwStreet.setCreateId(loginUserId);
            fwStreet.setCreateTime(LocalDateTime.now());
            fwStreetService.save(fwStreet);
        }else {
            fwStreet.setUpdateId(loginUserId);
            fwStreet.setUpdateTime(LocalDateTime.now());
            fwStreetService.updateById(fwStreet);
        }
        return Result.success("操作成功！");
    }

}

